package com.alinesno.cloud.initializr.starter.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.alinesno.cloud.common.core.service.impl.IBaseServiceImpl;
import com.alinesno.cloud.common.facade.constants.FieldConstants;
import com.alinesno.cloud.initializr.starter.dto.BingFormGitlabDto;
import com.alinesno.cloud.initializr.starter.entity.BuildGitEntity;
import com.alinesno.cloud.initializr.starter.enums.BingGitEnum;
import com.alinesno.cloud.initializr.starter.enums.GitRepositoryEnum;
import com.alinesno.cloud.initializr.starter.repository.BuildGitMapper;
import com.alinesno.cloud.initializr.starter.service.IBuildGitService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author alinesno
 * @date 2021-06-05
 */
@Service
public class BuildGitServiceImpl extends IBaseServiceImpl<BuildGitEntity, BuildGitMapper> implements IBuildGitService {
	// 日志记录
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(BuildGitServiceImpl.class);

	@Override
	public void bingGithub(String accountId, String githubAccessToken , JSONObject userInfo) {
		
		QueryWrapper<BuildGitEntity> wrapper = new QueryWrapper<BuildGitEntity>() ; 
		wrapper.eq(FieldConstants.OPERATOR_ID, accountId)
			   .eq("git_type", GitRepositoryEnum.GITHUB.getName()) ; 
	
		BuildGitEntity e = getOne(wrapper) ; 
	
		e.setAccessPrivateToken(githubAccessToken);
		e.setHasBing(BingGitEnum.HAS_BING.getV());
		e.setBingGitInfo(userInfo.toJSONString());
	
		this.update(e);
	}

	@Override
	public void unBingGit(String id , String gitType) {
		
		BuildGitEntity e = getById(id) ; 
		
		if(GitRepositoryEnum.GITHUB.getName().equals(e.getGitType())) { // 解绑github
			
			e.setHasBing(BingGitEnum.NOT_BING.getV());
			this.update(e);
			
		} else if(GitRepositoryEnum.GITEE.getName().equals(e.getGitType())) { // 解绑gitee
			
			e.setHasBing(BingGitEnum.NOT_BING.getV());
			this.update(e);
			
		} else if(GitRepositoryEnum.GITLAB.getName().equals(e.getGitType())) { // 解绑gitlab
			
			e.setHasBing(BingGitEnum.NOT_BING.getV());
			this.update(e);
			
		} else if(GitRepositoryEnum.GITEA.getName().equals(e.getGitType())) { // 解绑gitea
			
			e.setHasBing(BingGitEnum.NOT_BING.getV());
			this.update(e);
			
		}
		
	}

	@Override
	public void bingGitee(String accountId, JSONObject accessToken, JSONObject userInfo) {
		
		QueryWrapper<BuildGitEntity> wrapper = new QueryWrapper<BuildGitEntity>() ; 
		wrapper.eq(FieldConstants.OPERATOR_ID, accountId)
			   .eq("git_type", GitRepositoryEnum.GITEE.getName()) ; 
	
		BuildGitEntity e = getOne(wrapper) ; 
	
		e.setAccessPrivateToken(accessToken.getString("access_token"));
		e.setRefreshToken(accessToken.getString("refresh_token"));
		e.setExpiresIn(accessToken.getIntValue("expires_in"));
		
		e.setHasBing(BingGitEnum.HAS_BING.getV());
		e.setBingGitInfo(userInfo.toJSONString());
	
		this.update(e);
		
	}

	@Override
	public void bingGitlab(String accountId, BingFormGitlabDto dto) {

		String gitlabId = dto.getGitlabId() ; 
		
		BuildGitEntity e = getById(gitlabId) ; 

		e.setGitHost(dto.getGitHost());
		e.setGitUsername(dto.getUserName());
		e.setAccessPrivateToken(dto.getAccessPrivateToken());
		e.setHasBing(BingGitEnum.HAS_BING.getV());
	
		this.update(e);
		
	}

	@Override
	public void bingGitea(String accountId, JSONObject accessToken, JSONObject userInfo) {
		
		QueryWrapper<BuildGitEntity> wrapper = new QueryWrapper<BuildGitEntity>() ; 
		wrapper.eq(FieldConstants.OPERATOR_ID, accountId)
			   .eq("git_type", GitRepositoryEnum.GITEA.getName()) ; 
	
		BuildGitEntity e = getOne(wrapper) ; 
	
		e.setAccessPrivateToken(accessToken.getString("access_token"));
		e.setRefreshToken(accessToken.getString("refresh_token"));
		e.setExpiresIn(accessToken.getIntValue("expires_in"));
		
		e.setHasBing(BingGitEnum.HAS_BING.getV());
		e.setBingGitInfo(userInfo.toJSONString());
	
		this.update(e);
	}

}












